#pragma once
#include <bits/stdc++.h>
using namespace std;

struct HashBKDR
{
    // BKDR
    size_t operator()(const string &key)
    {
        size_t val = 0;
        for (auto ch : key)
        {
            val *= 131;
            val += ch;
        }

        return val;
    }
};

struct HashAP
{
    // BKDR
    size_t operator()(const string &key)
    {
        size_t hash = 0;
        for (size_t i = 0; i < key.size(); i++)
        {
            if ((i & 1) == 0)
            {
                hash ^= ((hash << 7) ^ key[i] ^ (hash >> 3));
            }
            else
            {
                hash ^= (~((hash << 11) ^ key[i] ^ (hash >> 5)));
            }
        }
        return hash;
    }
};

struct HashDJB
{
    // BKDR
    size_t operator()(const string &key)
    {
        size_t hash = 5381;
        for (auto ch : key)
        {
            hash += (hash << 5) + ch;
        }

        return hash;
    }
};
namespace m_std
{
    template <size_t N, class K = string, class Hash1 = HashBKDR, class Hash2 = HashAP, class Hash3 = HashDJB>
    class BloomFilter
    {
    public:
        void Set(const K& key)
        {
            size_t hashI1 = Hash1()(key) % (_ratio*N);
            _bits->set(hashI1);

            size_t hashI2 = Hash2()(key) % (_ratio * N);
            _bits->set(hashI2);

            size_t hashI3 = Hash3()(key) % (_ratio * N);
            _bits->set(hashI3);
        }
        void Reset(const K& key)
        {
            
        }
        bool Test(const K& key)
        {
            size_t hash1 = Hash1()(key) % (_ratio * N);
            // cout << hash1 << endl;
            if (!_bits->test(hash1))
                return false; // 准确的

            size_t hash2 = Hash2()(key) % (_ratio * N);
            // cout << hash2 << endl;

            if (!_bits->test(hash2))
                return false; // 准确的

            size_t hash3 = Hash3()(key) % (_ratio * N);
            // cout << hash3 << endl;

            if (!_bits->test(hash3))
                return false; // 准确的

            return true; // 可能存在误判
        }

    private:
        const static size_t _ratio = 5;                    //控制整体的过滤器长度
        bitset<_ratio *N> *_bits = new bitset<_ratio * N>; //位图
    };
    void TestBloomFilter1()
    {
        BloomFilter<10> bf;
        string arr1[] = {"苹果", "西瓜", "阿里", "美团", "苹果", "字节", "西瓜", "苹果", "香蕉", "苹果", "腾讯"};

        for (auto &str : arr1)
        {
            bf.Set(str);
        }

        for (auto &str : arr1)
        {
            cout << bf.Test(str) << endl;
        }
        cout << endl
             << endl;

        string arr2[] = {"苹果111", "西瓜", "阿里2222", "美团", "苹果dadcaddxadx", "字节", "西瓜sSSSX", "苹果 ", "香蕉", "苹果$", "腾讯"};

        for (auto &str : arr2)
        {
            cout << str << ":" << bf.Test(str) << endl;
        }
    }
};